package com.atguigu.BC;


import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.security.Security;
/**
 * @author : wushikai
 * <p>
 * date : 2021-04-22
 */
public enum EncryptUtils {

    /**
     * SINGLETON
     */
    SINGLETON;

    private static final String SECRET = "throwable";
    private static final String CHARSET = "UTF-8";

    //装载BC提供商
    static {
        Security.addProvider(new BouncyCastleProvider());
    }


    private Cipher createAesCipher() throws Exception {
        return Cipher.getInstance("AESWRAP");
    }

    public String encryptByAes(String raw) throws Exception {
        Cipher aesCipher = createAesCipher();
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AESWRAP");
        keyGenerator.init(128, new SecureRandom(SECRET.getBytes(CHARSET)));
        SecretKey secretKey = keyGenerator.generateKey();
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AESWRAP");
        aesCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] bytes = aesCipher.doFinal(raw.getBytes(CHARSET));
        return Hex.encodeHexString(bytes);
    }

    public String decryptByAes(String raw) throws Exception {
        byte[] bytes =   Hex.decodeHex(raw.toCharArray());
        Cipher aesCipher = createAesCipher();
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AESWRAP");
        keyGenerator.init(128, new SecureRandom(SECRET.getBytes(CHARSET)));
        SecretKey secretKey = keyGenerator.generateKey();
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AESWRAP");
        aesCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return new String(aesCipher.doFinal(bytes), CHARSET);
    }

    public static void main(String[] args) throws Exception {
        //String raw = "throwable-a-doge";
        String raw = "12345678";

        String en = EncryptUtils.SINGLETON.encryptByAes(raw);
        System.out.println(en);
        String de = EncryptUtils.SINGLETON.decryptByAes(en);
        System.out.println(de);
    }
}
